package ee.maoinvest.frontend.controller.command.enterpriserelationswithelectricitymetersandcontractscommands;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import ee.maoinvest.backend.model.data.ElectricityContract;
import ee.maoinvest.backend.model.data.ElectricityMeter;
import ee.maoinvest.backend.model.data.Enterprise;
import ee.maoinvest.backend.services.ElectricityContractService;
import ee.maoinvest.backend.services.ElectricityMeterService;
import ee.maoinvest.backend.services.EnterpriseService;
import ee.maoinvest.backend.services.implementations.ElectricityContractServiceImpl;
import ee.maoinvest.backend.services.implementations.ElectricityMeterServiceImpl;
import ee.maoinvest.backend.services.implementations.EnterpriseServiceImpl;
import ee.maoinvest.frontend.controller.command.Command;
import ee.maoinvest.frontend.controller.formstringdataconverters.enterpriserelationswithelectricitymetersandcontractsforms.InsertNewContractBetweenEnterpriseAndElectricityMeterFormConverter;
import ee.maoinvest.frontend.controller.requestprocessors.EnterpriseRelationsWithElectricityMeterAndContractFormsRequestProcessor;

public class FillInsertNewContractBetweenEnterpriseAndElectricityMeterFormConverterWithInitialDataCommand implements Command
{
	private int operationResult = 0;
	private final int OPERATION_SUCCESSFUL = 1;
	private final int OPERATION_UNSUCCESSFUL = 0;
	private final int ID_PARSE_ERROR_IDENTIFICATOR = -1;
	
	@Override
	public int executeCommand(HttpServletRequest request,
			HttpServletResponse response) 
	{
		EnterpriseRelationsWithElectricityMeterAndContractFormsRequestProcessor enterpriseRelationsWithElectricityMeterAndContractFormsRequestProcessor = 
				new EnterpriseRelationsWithElectricityMeterAndContractFormsRequestProcessor();
		InsertNewContractBetweenEnterpriseAndElectricityMeterFormConverter insertNewContractBetweenEnterpriseAndElectricityMeterFormConverter = null;
		Enterprise enterpriseWhichRelationsAreBeingModified = null;
		List<ElectricityMeter> listOfAllElectricityMetersValidToMakeContractWith = null;
		List<ElectricityContract> listOfAllElectricityContracts = null;

		EnterpriseService enterpriseServiceImpl = null;
		ElectricityMeterService electricityMeterServiceImpl = null;
		ElectricityContractService electricityContractServiceImpl = null;
		int enterpriseIDWithWhoNewContractWithElectricityMeterIsToBeInserted = 
				enterpriseRelationsWithElectricityMeterAndContractFormsRequestProcessor.getEnterpriseIDWithWhoNewContractWithElectricityMeterIsToBeInserted(request);
		
		
		if (enterpriseIDWithWhoNewContractWithElectricityMeterIsToBeInserted != ID_PARSE_ERROR_IDENTIFICATOR)
		{
			insertNewContractBetweenEnterpriseAndElectricityMeterFormConverter = 
					new InsertNewContractBetweenEnterpriseAndElectricityMeterFormConverter();
			enterpriseServiceImpl = new EnterpriseServiceImpl();
			electricityMeterServiceImpl = new ElectricityMeterServiceImpl();
			electricityContractServiceImpl = new ElectricityContractServiceImpl();
			listOfAllElectricityMetersValidToMakeContractWith = new ArrayList<ElectricityMeter>();
			listOfAllElectricityContracts = new ArrayList<ElectricityContract>();
			
			enterpriseWhichRelationsAreBeingModified = enterpriseServiceImpl.getEnterpriseById(
					enterpriseIDWithWhoNewContractWithElectricityMeterIsToBeInserted);
			listOfAllElectricityMetersValidToMakeContractWith = electricityMeterServiceImpl.getAllElectricityMetersThatHaveNoChildMetersAndAreNotBoundedWithContractElseThanDefaultContract();
			listOfAllElectricityContracts = electricityContractServiceImpl.getAllElectricityContractsForLocalTerritoryElectricityMetersThatAreNotWrittenOff();
			insertNewContractBetweenEnterpriseAndElectricityMeterFormConverter.fillWithInitialData(
					enterpriseWhichRelationsAreBeingModified, listOfAllElectricityMetersValidToMakeContractWith,
						listOfAllElectricityContracts);
			request.setAttribute("insertNewContractBetweenEnterpriseAndElectricityMeterFormConverter", 
					insertNewContractBetweenEnterpriseAndElectricityMeterFormConverter);
			
			HttpSession session = request.getSession();
			session.setAttribute("enterpriseWhichRelationsAreBeingModified", enterpriseWhichRelationsAreBeingModified);
			session.setAttribute("listOfAllElectricityMetersValidToMakeContractWith", listOfAllElectricityMetersValidToMakeContractWith);
			session.setAttribute("listOfAllElectricityContracts", listOfAllElectricityContracts);
			operationResult = OPERATION_SUCCESSFUL;
			
		}
		else
		{
			operationResult = OPERATION_UNSUCCESSFUL;
		}
		
		return operationResult;
	}
}
